from web.forms.account import UserForm,UserresetForm,UserRegisterForm
import json
from django.shortcuts import render, redirect
from web import models
from utils.block import BlockChainOperation


def user_login(request):
    if request.method == "GET":
        form = UserForm(request)
        return render(request, 'user_login.html', {'form': form})
    form = UserForm(request, data=request.POST)
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']

        user_object = models.CustomerInfo.objects.filter(username=username).first()
        if not user_object:
            form.add_error(f'username', '账号或密码错误！')
            return render(request, 'user_login.html', {'form': form})
        if user_object.password == password:
            request.session['customer_name'] = user_object.username
            request.session.set_expiry(60 * 60 * 24 * 14)
            if user_object.is_first == 1:
                return render(request,'alert.html')
            return redirect('user_info')
    return render(request, 'user_login.html', {'form': form})

def user_reset(request):
    if request.method == 'GET':
        form = UserresetForm(request)
        return render(request, 'user_reset.html', {'form':form})
    form = UserresetForm(request,data=request.POST)
    if form.is_valid():
        models.CustomerInfo.objects.filter(username=form.cleaned_data['username']).update(password=form.cleaned_data['password'])
        models.CustomerInfo.objects.filter(username=form.cleaned_data['username']).update(is_first=0)
        form = UserForm(request)
        return render(request, 'user_login.html', {'form': form})
    return render(request, 'user_login.html', {'form': form})


def user_register(request):
    if request.method == "GET":
        form = UserRegisterForm(request)
        return render(request, 'user_register.html', {'form': form})
    form = UserRegisterForm(request, data=request.POST)
    if form.is_valid():
        form.save()
        return redirect('user_login')
    return render(request, 'user_register.html', {'form': form})


def user_logout(request):
    request.session.flush()
    return redirect('user_login')

def userinfo(request):
    customer_object = request.customer
    customer_id = customer_object.username
    blocks_data = models.Block.objects.filter(customer_id=customer_id).all()

    return render(request,'user_info.html',{'project_dict':blocks_data})


def user_blockinfo(request):
    id = request.GET.get('id')
    # order_id = request.GET.get('order_id')
    # customer_id = request.GET.get('customer_id')
    # product_id = request.GET.get('product_id')
    block_info = models.Block.objects.filter(id=id).first()
    block = block_info.block
    real_hash = BlockChainOperation.getRealHash(id)
    now_hash = BlockChainOperation.getNowHash(block)
    block = json.loads(block)
    order = json.loads(block["data"])
    import qrcode
    img = qrcode.make(real_hash)
    img.save("web/static/image/some_file.png")
    return render(request, 'user_blockinfo.html', {"order": order, "real_hash": real_hash, "now_hash": now_hash})